package everyday;

import org.junit.jupiter.api.Test;
import tool.s;

import java.util.Arrays;

public class T1040 {
    public int[] numMovesStonesII(int[] stones) {
        Arrays.sort(stones);
        int len = stones.length;
        int left = 0;
        int right = 0;
        int max=Math.max(stones[len-1]-stones[1],stones[len-2]-stones[0])+1-(len-1);
        int min=len;
        while (right != len-1) {
            while (right+1 < len && stones[right+1] - stones[left] < len) {
                right++;
            }
            if (right-left+1==(len-1)&&stones[right]-stones[left]+1==(len-1)){
                min=Math.min(min,2);
            }else {
                min=Math.min(min,len-(right-left+1));
            }
            left++;
        }
        return new int[]{min,max};
    }
    @Test
    public void test() {
        int []ints={3,4,5,6,10};
        int [] res=numMovesStonesII(ints);
        s.print(res);
    }
}
